//
//  ShowCountCell.swift
//  HappyEat
//
//  Created by 张继强 on 17/1/23.
//  Copyright © 2017年 陌路是我. All rights reserved.
//

import UIKit

protocol ShowCountCellDelegate {
    func returnBuyNumCount(_ countStr:String)
}

class ShowCountCell: UITableViewCell {
    
    let countStrs:[String] = ["1","5","10","20"]
//    var joinLabel:UILabel?{
//        return UILabel.init()
//    }
//    var tagLabel:UILabel?{
//        return UILabel.init()
//    }
//    var countDownBt:UIButton?{
//        return UIButton.init()
//    }
//    var countUpBt:UIButton?{
//        return UIButton.init()
//    }
//    var countLabel:UILabel?{
//        return UILabel.init()
//    }
    var joinLabel:UILabel = UILabel.init()
    var tagLabel:UILabel = UILabel.init()
    var countDownBt:UIButton = UIButton.init()
    var countUpBt:UIButton = UIButton.init()
    var countLabel:UILabel = UILabel.init()
    var duihuanLabel:UILabel = UILabel.init()

    var delegate:ShowCountCellDelegate?
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
extension ShowCountCell {
    func setUI(){
        self.contentView.addSubview(self.joinLabel)
        self.contentView.addSubview(self.tagLabel)
        self.contentView.addSubview(self.countLabel)
        self.contentView.addSubview(self.countDownBt)
        self.contentView.addSubview(self.countLabel)
        self.contentView.addSubview(self.countUpBt)
        self.countDownBt.addTarget(self, action: #selector(countDownAction), for: .touchUpInside)
        self.countUpBt.addTarget(self, action: #selector(countUpAction), for: .touchUpInside)
        self.joinLabel.snp.makeConstraints({ (make) in
            make.left.equalTo(16)
            make.top.equalTo(12)
            make.width.equalTo(100)
            make.height.equalTo(16)
        })
        self.joinLabel.text = "参与人次"
        self.joinLabel.font = UIFont.systemFont(ofSize: 16)
        self.tagLabel.snp.makeConstraints { (make) in
            make.right.equalTo(-16)
            make.top.equalTo(27)
            make.width.equalTo(200)
            make.height.equalTo(12)
        }
        self.tagLabel.text = "1象牙每人次"
        self.tagLabel.textColor = BaseFontColor
        self.tagLabel.textAlignment = .right
        self.tagLabel.font = UIFont.systemFont(ofSize: 12)
        self.countLabel.snp.makeConstraints({ (make) in
            make.size.equalTo(CGSize.init(width: 120, height: 40))
            make.left.equalTo((k_Wide - 120)/2)
            make.top.equalTo(38.5)
        })
        self.countLabel.layer.borderColor = UIColor.red.cgColor
        self.countLabel.layer.borderWidth = 1.0
        self.countLabel.textAlignment = .center
        self.countLabel.backgroundColor = .white
        self.countLabel.textColor = .red
        self.countLabel.text = "10"
           self.countDownBt.snp.makeConstraints({ (make) in
            make.size.equalTo(CGSize.init(width: 20, height: 20))
            make.top.equalTo((self.countLabel.snp.top)).offset(10)
            make.right.equalTo(self.countLabel.snp.left).offset(-4)
        })
        self.countUpBt.snp.makeConstraints({ (make) in
            make.size.equalTo(CGSize.init(width: 20, height: 20))
            make.top.equalTo(self.countDownBt)
            make.left.equalTo(self.countLabel.snp.right).offset(4)
        })
        self.countUpBt.setImage(UIImage.init(named: "ic_+"), for: .normal)
        self.countDownBt.setImage(UIImage.init(named: "ic_-"), for: .normal)
        for i in 0 ..<  self.countStrs.count {
            let bt:UIButton = UIButton.init()
            let itemWidth:CGFloat = k_Wide/4
            let bgView:UIView = UIView.init()
            self.addSubview(bgView)
            bgView.addSubview(bt)
            bgView.snp.makeConstraints({ (make) in
                make.left.equalTo(CGFloat(i) * itemWidth)
                make.size.equalTo(CGSize.init(width: itemWidth, height:25))
                make.top.equalTo(self.countLabel.snp.bottom).offset(12)
            })
            bt.layer.borderColor = BaseFontColor.cgColor
            bt.layer.borderWidth = 1.0
            bt.backgroundColor = .white
            bt.setTitleColor(.lightGray, for: .normal)
            bt.snp.makeConstraints({ (make) in
                make.left.equalTo(16)
                make.height.equalTo(25)
                make.right.equalTo(-16)
                make.top.equalTo(0)
            })
            bt.setTitle(countStrs[i], for: .normal)
            bt.tag = 1000+i
            bt.addTarget(self, action: #selector(btAction(_:)), for: .touchUpInside)
        }
        let fgView:UIView = UIView.init()
        self.contentView.addSubview(fgView)
        fgView.snp.makeConstraints { (make) in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(0.5)
            make.top.equalTo(self.countLabel.snp.bottom).offset(49)
        }
        fgView.backgroundColor  = .lightGray
        self.contentView.addSubview(self.duihuanLabel)
        self.duihuanLabel.snp.makeConstraints { (make) in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(16)
            make.top.equalTo(fgView.snp.bottom).offset(12)
        }
        self.duihuanLabel.textAlignment = .center
        self.duihuanLabel.text = "兑换总额:10象牙"
        self.duihuanLabel.font = UIFont.systemFont(ofSize: 16)
    }
    func btAction(_ button:UIButton){
        if button.tag == 1000{
            //1
            self.countLabel.text = "1"
            self.duihuanLabel.text = "兑换总额:1象牙"
            delegate?.returnBuyNumCount(self.countLabel.text!)
        }else if button.tag == 1001{
            //5
            self.countLabel.text = "5"
            self.duihuanLabel.text = "兑换总额:5象牙"
            delegate?.returnBuyNumCount(self.countLabel.text!)
        }else if button.tag == 1002{
            //10
            self.countLabel.text = "10"
            self.duihuanLabel.text = "兑换总额:10象牙"
            delegate?.returnBuyNumCount(self.countLabel.text!)
        }else {
            //20
            self.countLabel.text = "20"
            self.duihuanLabel.text = "兑换总额:20象牙"
            delegate?.returnBuyNumCount(self.countLabel.text!)
        }
    }
    func countDownAction(){
       //减低事件
        var countStr:NSString = NSString.init(string: self.countLabel.text!)
        var count:Int = countStr.integerValue
        if count <= 1 {
            //不改变状态
            return
        }else{
            count = count - 1
            let tempStr:NSString = NSString.init(format: "%i", count)
            self.countLabel.text = tempStr as String
            self.duihuanLabel.text = "兑换总额:" + (tempStr as String) + "象牙"
            delegate?.returnBuyNumCount(self.countLabel.text!)
        }
    }
    func countUpAction(){
        //增加事件
        let countStr:NSString = NSString.init(string: self.countLabel.text!)
        let count:Int = countStr.integerValue + 1
        let temStr:NSString = NSString.init(format: "%i", count)
        self.countLabel.text = temStr as String
        self.duihuanLabel.text = "兑换总额:" + (temStr as String) + "象牙"
        delegate?.returnBuyNumCount(self.countLabel.text!)
    }
}
